﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FileModelNS
{
	public class FileContents
	{
		public string FileName { get { return _FileName; } }
		public int Length { get { return _Size; } }
		public byte[] Data { get { return _Data; } }

		private int _Size;
		private string _FileName;
		private byte[] _Data;

		public FileContents(string path)
		{
			using (FileStream stream = File.OpenRead(path))
			{
				BinaryReader reader = new BinaryReader(stream, Encoding.ASCII);
				int size = (int)stream.Seek(0, SeekOrigin.End);

				stream.Seek(0, SeekOrigin.Begin);
				byte[] data = new byte[size];
				reader.Read(data, 0, size);

				// Succeeded
				_Size = (int)size;
				_FileName = path;
				_Data = data;
			}
		}

		public byte[] CopyBytes(int offset, int length)
		{
			if ( _Data.Length <= offset || Data.Length < offset + length )
				throw new Exception("Invalid Range");

			byte[] ret = new byte[length];
			Buffer.BlockCopy( _Data, (int)offset, ret, 0, (int)length );
			return ret;
		}
	}
}
